using System; 
using System.Text;
using System.Data;
using System.Data.Common;
using System.Collections;
using Kcitwm.Utilities;
using System.Configuration;

namespace Kcitwm.DataHelper
{
    public class CommonHelper
    {
        protected bool disposed = false;
        public IDbConnection conn;
        protected IDbCommand cmd;
        protected DbProviderFactory df;

        bool closeConn = true;
         
        private ConnectionStringSettings connStringSettins;         

        public ConnectionStringSettings ConnStringSettins
        {
            get { return connStringSettins; }
            set { connStringSettins = value; }
        }

        public bool CloseConn
        {
            get { return closeConn; }
            set { closeConn = value; }
        }

        public CommonHelper()
        {

        }

        public CommonHelper(ConnectionStringSettings connStringSettins)
        {
            this.connStringSettins = connStringSettins;
            Connect();
        }

        public CommonHelper(ConnectionStringSettings connStringSettins, bool closeConn):this(connStringSettins)
        {
            this.closeConn = closeConn; 
        }


        virtual public void Connect()
        {
            try
            {
                df = DbProviderFactories.GetFactory(connStringSettins.ProviderName);
                conn = df.CreateConnection();
                conn.ConnectionString = connStringSettins.ConnectionString;
                cmd = conn.CreateCommand();
            }
            catch (Exception e)
            { 
                Utilities.Log.Write("CommonProvider.Connect:" + e.Message);
            }
        }


        public IDbDataParameter AddParameters(string name, DbType type)
        {
            IDbDataParameter param = cmd.CreateParameter();
            param.ParameterName = name;
            param.DbType = type;
            cmd.Parameters.Add(param);
            return param;
        }

        public IDbDataParameter AddParameters(string name, DbType type, int size)
        {
            IDbDataParameter param = cmd.CreateParameter();
            param.ParameterName = name;
            param.DbType = type;
            param.Size = size;
            cmd.Parameters.Add(param);
            return param;
        }

        public IDbDataParameter AddParameters(string name, DbType type, int size, string sourceColumn, DataRowVersion version, ParameterDirection direction)
        {
            IDbDataParameter param = cmd.CreateParameter();
            param.ParameterName = name;
            param.DbType = type;
            param.Size = size;
            param.SourceColumn = sourceColumn;
            param.SourceVersion = version;
            param.Direction = direction;
            cmd.Parameters.Add(param);
            return param;
        }

        public IDbDataParameter AddParameters(string name, DbType type, string sourceColumn, DataRowVersion version, ParameterDirection direction)
        {
            IDbDataParameter param = cmd.CreateParameter();
            param.ParameterName = name;
            param.DbType = type;
            param.SourceColumn = sourceColumn;
            param.SourceVersion = version;
            param.Direction = direction;
            cmd.Parameters.Add(param);
            return param;
        }



        public IDbDataParameter AddParameters(string name, object o)
        {
            IDbDataParameter param = cmd.CreateParameter();
            param.ParameterName = name;
            param.Value = o;
            cmd.Parameters.Add(param);
            return param;
        }

        public IDbDataParameter AddParameters(string name, object o, DbType type)
        {
            IDbDataParameter param = cmd.CreateParameter();
            param.ParameterName = name;
            param.Value = o;
            param.DbType = type;
            cmd.Parameters.Add(param);
            return param;
        }

        public IDbDataParameter AddParameters(string name, object o, ParameterDirection d)
        {
            IDbDataParameter param = cmd.CreateParameter();
            param.ParameterName = name;
            param.Value = o;
            param.Direction = d;
            cmd.Parameters.Add(param);
            return param;
        }

        public IDbDataParameter AddParameters(string name, DbType type, ParameterDirection d)
        {
            IDbDataParameter param = cmd.CreateParameter();
            param.ParameterName = name;
            param.DbType = type;
            param.Direction = d;
            cmd.Parameters.Add(param);
            return param;
        }

        public IDbDataParameter AddParameters(string name, DbType type, ParameterDirection d, int size)
        {
            IDbDataParameter param = cmd.CreateParameter();
            param.ParameterName = name;
            param.DbType = type;
            param.Direction = d;
            param.Size = size;
            cmd.Parameters.Add(param);
            return param;
        }



        public IDataParameterCollection Parameter
        {
            get
            {
                return cmd.Parameters;
            }
        }


        public void ClearParameters()
        {
            if (cmd.Parameters.Count > 0)
                cmd.Parameters.Clear();
        }

        public int ExecuteNonQuery(string cmdText, CommandType cmdType)
        {
            cmd.CommandText = cmdText;
            cmd.CommandType = cmdType;
            int n = -1;
            try
            {
                if (conn.State != ConnectionState.Open)
                    conn.Open();
                n = cmd.ExecuteNonQuery();
                ClearParameters();
                if (closeConn)
                    conn.Close();
            }
            catch (Exception e)
            {
                if (conn.State == ConnectionState.Open)
                    conn.Close();
                Log.Write("ExecuteNonQuery.cmdText:" + cmdText + ":" + e.Message);
            }
            return n;
        }

        public object ExecuteScalar(string cmdText, CommandType cmdType)
        {
            //conn = new SqlConnection(cns);
            cmd.CommandText = cmdText;
            cmd.CommandType = cmdType;
            object o = null;
            try
            {
                if (!(conn.State == ConnectionState.Open))
                    conn.Open();
                o = cmd.ExecuteScalar();
                ClearParameters();
                if (closeConn)
                    conn.Close();
            }
            catch (Exception e)
            {
                if (conn.State == ConnectionState.Open)
                    conn.Close();
                Log.Write("ExecuteScalar.cmdText:" + cmdText + ":" + e.Message);
            }
            return o;
        }

        public IDataReader ExcuteDataReader(string cmdText, CommandType cmdType)
        {
            //Log.Write("Begin ExcuteDataReader.cmdText:" + cmdText);
            //conn = new SqlConnection(cns);
            cmd.CommandText = cmdText;
            cmd.CommandType = cmdType;
            IDataReader reader = null;
            try
            {
                if (conn.State == ConnectionState.Closed)
                    conn.Open();
                reader = cmd.ExecuteReader();
                ClearParameters();
            }
            catch (Exception e)
            {
                if (conn.State == ConnectionState.Open)
                    conn.Close();
                Log.Write("ExcuteDataReader.cmdText:" + cmdText + ":" + e.Message);
            }
            return reader;
        }

        public DataSet ExcuteDataSet(string cmdText, CommandType cmdType, string tableName)
        {
            //conn = new SqlConnection(cns);
            cmd.CommandText = cmdText;
            cmd.CommandType = cmdType;
            System.Data.DataSet ds = null;
            try
            {
                if (conn.State == ConnectionState.Closed)
                    conn.Open();
                IDbDataAdapter da = df.CreateDataAdapter();
                da.SelectCommand = cmd;
                ds = new DataSet(tableName);
                da.Fill(ds);
                ClearParameters();
                if (closeConn)
                    conn.Close();
            }
            catch (Exception e)
            {
                if (conn.State == ConnectionState.Open)
                    conn.Close();
                Log.Write("ExcuteDataSet.cmdText:" + cmdText + ":" + e.Message);
            }
            return ds;
        }

        public void Dispose()
        {
            Dispose(true);
        }

        public void Close()
        {
            if (null != conn && conn.State == ConnectionState.Open)
                conn.Close();
            if (null != cmd)
                cmd.Dispose();
        }

        public void Dispose(bool disposing)
        {
            if (!disposed)
            {
                if (disposing)
                {
                    if (conn.State == ConnectionState.Open)
                        conn.Close();
                    cmd.Dispose();
                    disposed = true;
                    GC.SuppressFinalize(this);
                }
            }
        }

    }
}
